package com.ppmall.util;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @Description:  手动获取Spring中的bean  注意这个类要一定交给spring来管理，注册到spring的配置文件中
 * 	spring-db-context.xml：<bean class="com.xxx.SpringApplicationContextHolder" />
 */
public class SpringApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringApplicationContextHolder.context = context;
    }


    public static Object getSpringBean(String beanName) {
        if (StringUtils.isNotBlank(beanName)) {
            return context==null?null:context.getBean(beanName);
        }

        return null;

    }

    public static String[] getBeanDefinitionNames() {
        return context.getBeanDefinitionNames();
    }
}
